//单链表-》**指向 遍历
public class MySingleList implements ILinkedList {
    //定义静态内部类来表示节点引用
    static class ListNode{
        //数据
        public int val;
        //节点
        public ListNode next;

        public ListNode(int val){
            this.val = val;
        }
    }

    //头节点
    private ListNode head;

    public void createList(){
        ListNode node1 = new ListNode(12);
        ListNode node2 = new ListNode(23);
        ListNode node3 = new ListNode(34);
        ListNode node4 = new ListNode(45);
        ListNode node5 = new ListNode(56);

        node1.next = node2;
        node2.next = node3;
        node3.next = node4;
        node4.next = node5;

        head = node1;
    }


    @Override
    public void addFirst(int data) {
        ListNode node = new ListNode(data);

        node.next = head;
        head = node;
    }

    @Override
    public void addLast(int data) {
        ListNode node = new ListNode(data);
        //链表为空情况
        if(head == null){
            head = node;
            return;
        }
        //先找到链表的尾巴
        ListNode cur = head;
        while (cur.next !=null){
            cur = cur.next;
        }
        //此时cur指向的节点就是链表最后节点
        cur.next = node;
    }


    @Override
    public void addIndex(int index, int data) {
        checkPos(index);
        if (index == 0){
            addFirst(data);
            return;
        }
        if (index == size()){
            addLast(data);
            return;
        }
        //中间插入
        ListNode cur = findIndex(index);
        ListNode node = new ListNode(data);
        node.next = cur.next;
        cur.next = node;
    }

    private ListNode findIndex(int index){
        ListNode cur = head;
        int count = 0;
        while (count != index-1){
            cur = cur.next;
            count++;
        }
        return cur;
    }

    private void checkPos(int index){
        if(index <0 || index >size()){
            throw new CheckPosException("index的位置不合法: "+index);
        }
    }

    @Override
    public boolean contains(int key) {
        ListNode cur = head;
        while (cur !=null){
            if(cur.val == key){
                return true;
            }
            cur = cur.next;
        }
        return false;
    }

    @Override
    public void remove(int key) {
        if (head == null){
            System.out.println("此链表为空，无法删除");
            return;
        }
        if (head.val == key){
            head = head.next;
            return;
        }
        ListNode cur = search(key);
        if (cur == null){
            System.out.println("未找到删除的数字");
            return;
        }

        ListNode del = cur.next;
        cur.next = del.next;

    }

    private ListNode search(int key){
        ListNode cur = head;
        while (cur.next !=null){
            if(cur.next.val == key){
                return cur;
            }
            cur = cur.next;
        }
        return null;
    }

    @Override
    public void removeAllkey(int key) {
        if (head == null){
            return;
        }

        ListNode pre = head;
        ListNode cur = head.next;
        while (cur != null){
            if (cur.val == key){
                pre.next = cur.next;
            }else {
                pre = cur;
            }
            cur = cur.next;
        }

        if (head.val == key){
            head = head.next;
        }

    }

    @Override
    public int size() {
        ListNode cur = head;
        int count = 0;
        while (cur !=null){
            count++;
            cur = cur.next;
        }
        return count;
    }

    @Override
    public void clean() {
        ListNode cur = head;
        while (cur != null) {
            //cur.val = null;
            ListNode curN = cur.next;
            cur.next = null;
            cur = curN;
        }//双向链表中使用
        head = null;//可以直接head=null

    }

    @Override
    public void display() {
        ListNode cur = head;
        while (cur != null){
            System.out.print(cur.val+" ");
            cur = cur.next;
        }
        System.out.println();
    }
}
